package com.mls.fivedistrict.server.impl;

import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.entity.request.AwardRequest;
import com.mls.fivedistrict.enums.RecordStatusEnum;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Integral;
import com.mls.fivedistrict.pojo.Property;
import com.mls.fivedistrict.pojo.Record;
import com.mls.fivedistrict.server.AwardService;
import com.mls.fivedistrict.server.IntegralService;
import com.mls.fivedistrict.server.PropertyService;
import com.mls.fivedistrict.server.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * Created by chenxiuxiang on 2018/6/12.
 */
@Service("awardService")
public class AwardServiceImpl implements AwardService {
    @Autowired
    RecordService recordService;
    @Autowired
    PropertyService propertyService;
    @Autowired
    IntegralService integralService;

    @Override
    public void awardDwq(AwardRequest awardRequest) {
        //1.给目标用户发放奖励
        BigDecimal awardNumber = awardRequest.getAwardNumber();
        if (awardNumber.compareTo(new BigDecimal(0)) <= 0) {
            return;
        }
        Record record = new Record();
        record.setUserId(awardRequest.getTargetUserId());
        record.setValue(awardRequest.getAwardNumber());
        record.setIntergralId(awardRequest.getIntegralId());
        record.setSource(awardRequest.getSource());
        record.setChangeReason(awardRequest.getReason());
        record.setStatus(RecordStatusEnum.ALREADYRECEIVE.getValue());
        int insertRecord = recordService.insertRecord(record);
        if (insertRecord <= 0) {
            throw new CustomerException("插入记录出错");
        }
        //1.1添加资产
        Property propertyDetailByIntegral = propertyService.getPropertyDetailByIntegral(awardRequest.getIntegralId(), awardRequest.getTargetUserId());
        if (propertyDetailByIntegral == null) {
            Property property = new Property();
            property.setTotal(awardRequest.getAwardNumber());
            property.setIntegralId(awardRequest.getIntegralId());
            property.setUserId(awardRequest.getTargetUserId());
            boolean b = propertyService.addProperty(property);
            if (!b) {
                throw new CustomerException("添加资产出错");
            }
        } else {
            int i = propertyService.increatementPropertyTotal(awardRequest.getTargetUserId(), awardRequest.getIntegralId(), awardNumber);
            if (i <= 0) {
                throw new CustomerException("更新资产出错");
            }
        }
        //2.扣除dwq
        Integral integralDetail = integralService.getIntegralDetail(awardRequest.getIntegralId());
        Integral integral = new Integral();
        integral.setId(integralDetail.getId());
        integral.setUserIntegral(integralDetail.getUserIntegral().add(awardNumber).setScale(8, BigDecimal.ROUND_DOWN));
        int i = integralService.updateEdtegral(integral);
        if (i <= 0) {
            throw new CustomerException("扣除积分出错");
        }
    }
}
